import json
import os.path
import requests
import json
import pandas as pd
import os.path


# 请求头相关参数
headers = {
        'accept': 'application/json, text/plain, */*',
        'accept-language': 'zh-CN,zh;q=0.9',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
        # 添加其他必要的请求头，如认证信息
        'Cookie':'s_fid=722C4D05C185690C-0452A3CBE40FEA72; s_vi=[CS]v1|33C44E1C90E6D42F-6000197F41C07960[CE]; XID=727baf56bc1d46e0386d36e689760e2c; geo=KR; s_cc=true; s_sq=%5B%5BB%5D%5D; dslang=CN-ZH; site=CHN; myacinfo=DAWTKNV323952cf8084a204fb20ab2508441a07d02d3e6963dcad3e78babe59a8b761686f0ec67d495e00ca1ea7afb5852b75a9ad29dc4afd0085f62d1c316ce984c16875aa6be33fd0d367e7404b29962c2cea6f62eb5719762c25c7cfdd750ad6b4dd97dc584eecc60812d7e9b61b8c6c0b43ccb5dae5f1fa970f297bdb9bbfde3cf138918b5463764bb6374595f713a62fda8a52e6ddd78a3d70e234e7fecd0f5000911745b1196ea3e9c8a5ebfa16c9b3474b8eefd549bd4ceee5f4c7beb18ba642465d9a5da1c820ded37a02fcc62c0c60a858e813a2c1c153c503f619ed6d17fd889ca9ada3a5f30e50a87030ad0d245afa27c67e9f0d06b75920cf9928d78f52a62ef5db10c0a29ec2c8f097d3425b6c6e118aebcd2d8200e449ad0c7d802a71495a35f828a1f041c4efbccc4fd02eca84772fbb3725d5b34a46f8490302f6efa01d4627c436c7362e80a4d5cb12812672d15aeaeec8f84bce906121c9292a7f44e7dedcd1fa95fda6160186ea5dcc44a948e1eb4712c71175cf4a8b002731e1c0b89200876743b9a00f7900204cf70d994c8190cd48c4bd0481cb2814221928450bdfe827dd0aa831960c44e5dd6995871e44a8ab144d9e85d80d8277240ce76a260421e3e4c96291d67bf08bd5372736d81d5aceaf504e1ba7685c1a167a782955d372ce1ea733b20dddee944a3abfc1870f282f26ee5b5e5beba413b2f1f340f65f2be009906a8ae23fb0bbf27f3302f3a237da1a72b81c8220d1bc95f828b89a18aad243b6abca15eb3af7ff9360c92673515ce30c496742451eaf40bd85f3e280b9792bb798e7b5697d3c1e5911ae1584a5785e35f033c348281029167ca50dea34c5877910c8fc20f68fd0c0278ad8bcd2f2ce60299c5379c6da156d5e6bb70d5aa07e76d0ce9905ad82ff29f7ba822dddfb514566b21dce273c290bd374629771a07a22788e19a1a8c19a48e1bb23a5662273a2770dd4e3036cb805c6cffbc585a47V3; dc=pv; itctx=eyJjcCI6ImM2NjM4Y2I2LTdiNWYtNGFiYy05MjQ0LWJjMjUzYTI0Zjc0OSIsImRzIjoxODE0NTg4ODg2OCwiZXgiOiIyMDI1LTEtMTcgMTA6MToxMSJ9|2s6hscpael7mf62f3tgh7a4ccs|ePljP8giDV-hcCFKe-1l7JgLc_U; itcdq=0; dqsid=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3MzcwNzkyNzAsImp0aSI6IjdfNkFCSVJTWmFoT3o3bDZmU2tMV0EifQ.F3uJSIa2OAwBN9kEiZRbZKt1RXT5husT1Bt7kTECqj8; wosid=LObgYxYnxsy3hKPEEFTJ3g; woinst=5315'



}





# API的基础URL
base_url = 'https://appstoreconnect.apple.com/WebObjects/iTunesConnect.woa/ra/apps/6499516269/platforms/ios/reviews'




def fetch_reviews():

    rating_list = ['RATING_1', 'RATING_2', 'RATING_3', 'RATING_4', 'RATING_5']
    # rating_list = ['RATING_1', 'RATING_2']
    index_list1 = [0, 100, 200, 300]
    index_list2 = [0, 100, 200, 300,400,500,600,700,800,900]
    # index_list = [0,100]

    for rating in rating_list:

        if rating == 'RATING_5':
            current_index_list = index_list2
        else:
            current_index_list = index_list1
        for index in current_index_list:
            # 请求体相关参数
            params = {
                'rating': rating,  # 评分参数
                'storefront': 'US',  # 店铺地区
                'sort': 'REVIEW_SORT_ORDER_MOST_RECENT',  # 排序：最新
                'index': index,  # 分页参数，动态传入
            }
            response = requests.get(base_url, headers=headers, params=params)
            if response.status_code == 200:
                dic = response.json()
                with open(os.path.dirname(__file__) + '/data.txt','a+') as f:
                    for i in range(len(dic.get('data').get('reviews'))):
                        f.write(str(dic.get('data').get('reviews')[i].get('value').get('lastModified')) + '   ')
                        f.write(str(dic.get('data').get('reviews')[i].get('value').get('rating')) + '\n')
                # return response.json()

            else:
                print(f"请求失败，状态码: {response.status_code}，rating: {rating}, index: {index}")




if __name__ == '__main__':
    fetch_reviews()


import os
from datetime import datetime

from datetime import datetime

def count_ratings(start_time_str, end_time_str, file_path):
    # 定义时间格式
    datetime_format = '%Y-%m-%d %H:%M:%S'

    # 将字符串时间转换为datetime对象
    start_time = datetime.strptime(start_time_str, datetime_format)
    end_time = datetime.strptime(end_time_str, datetime_format)

    # 将datetime对象转换为时间戳（毫秒）
    start_timestamp = int(start_time.timestamp() * 1000)
    end_timestamp = int(end_time.timestamp() * 1000)

    # 初始化计数器
    rating_counts = {i: 0 for i in range(1, 6)}

    # 读取文件
    with open(file_path, 'r') as file:
        for line in file:
            # 去除行末的换行符
            line = line.strip()
            if not line:
                continue
            # 分割行数据
            timestamp_str, rating_str = line.split()
            # 将时间戳字符串转换为整数
            timestamp = int(timestamp_str)
            # 将评分字符串转换为整数
            rating = int(rating_str)
            # 检查时间范围和评分
            if start_timestamp <= timestamp <= end_timestamp and 1 <= rating <= 5:
                rating_counts[rating] += 1
                # print(f"Timestamp: {timestamp}, Rating: {rating}, Count: {rating_counts[rating]}")  # 调试信息

    return rating_counts


if __name__ == '__main__':

    # 示例调用
    start_time_str = '2025-01-13 00:00:00'
    end_time_str = '2025-01-15 23:59:59'
    file_path = 'data.txt'
    rating_counts = count_ratings(start_time_str, end_time_str, file_path)

    print("iOS - Haven商店评价统计及回复 （1.13 + 1.14）：")
    print("  - 差评1星：{}条；版本1.14.2".format(rating_counts.get(1, 0)))
    print("  - 差评2星：{}条；版本1.14.2".format(rating_counts.get(2, 0)))
    print("  - 中评3星：{}条；版本1.14.2".format(rating_counts.get(3, 0)))
    print("  - 好评4星：{}条；版本1.14.2".format(rating_counts.get(4, 0)))
    print("  - 好评5星：{}条；版本1.14.2".format(rating_counts.get(5, 0)))
    # print(rating_counts)

154 + 223 + 206